package net.arogarth.aws.android.manager.dbmodels;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import net.arogarth.aws.android.manager.AwsManager;
import net.arogarth.aws.android.manager.DatabaseAdapter;

public class AvailabilityZone extends DbObject {
	private Long id = null;
	private Long accountId = null;
	private String region = "";
	private String zone = "";
	private String state = "";
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getAccount_id() {
		return accountId;
	}

	public void setAccountId(Long account_id) {
		this.accountId = account_id;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getZone() {
		return zone;
	}

	public void setZone(String zone) {
		this.zone = zone;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	@Override
	public DbObject load(Long id) {
		@SuppressWarnings("unchecked")
		List<AwsAccount> list = (List<AwsAccount>) this.list(String.format("id = %s", id));
		if(list.size() > 0) return list.get(0);
		
		return null;
	}
	
	public List<?> list() {
		return list(String.format("account_id = %s", AwsManager.getCurrentAwsAccount().getId()));
	}

	@Override
	public List<?> list(String where) {
		String qry = "SELECT * FROM " + DatabaseAdapter.APP_DB_TABLE_AVAILABLE_ZONES;
		if(where != "")
			qry += " WHERE " + where;
		
		DatabaseAdapter dba = new DatabaseAdapter().open();
		Cursor c = dba.getDatabase().rawQuery(qry, new String[] {});
		
		List<AvailabilityZone> zones = new ArrayList<AvailabilityZone>();

		while(c.moveToNext()) {
			Long id = c.getLong(0);
			Long accountId = c.getLong(1);
			String region = c.getString(2);
			String zone = c.getString(3);
			String state = c.getString(4);
			
			AvailabilityZone oZone = new AvailabilityZone();
			oZone.setId(id);
			oZone.setAccountId(accountId);
			oZone.setRegion(region);
			oZone.setZone(zone);
			oZone.setState(state);
			zones.add(oZone);
		}
		
		dba.close();
		return zones;
	}

	@Override
	public void save() {
		DatabaseAdapter dba = new DatabaseAdapter().open();
		if(this.id == null) { // Save
			
			ContentValues values = new ContentValues();
			values.put("account_id", this.accountId);
			values.put("region", this.region);
			values.put("zone", this.zone);
			values.put("state", this.state);
			this.id = dba.getDatabase().insert(DatabaseAdapter.APP_DB_TABLE_AVAILABLE_ZONES, "0", values);
			
		} else { // Update
			
			String qry = "UPDATE " + DatabaseAdapter.APP_DB_TABLE_AVAILABLE_ZONES;
			qry += " SET ";
			qry += String.format("account_id = '%s',", this.accountId);
			qry += String.format("region = '%s',", this.region);
			qry += String.format("zone = '%s',", this.zone);
			qry += String.format("state = '%s',", this.state);
			qry += " WHERE id = " + this.id;
			dba.getDatabase().execSQL(qry);
			
		}
		
		dba.close();
	}

	@Override
	public void delete() {
		DatabaseAdapter dba = new DatabaseAdapter().open();
		dba.getDatabase().delete(DatabaseAdapter.APP_DB_TABLE_AVAILABLE_ZONES, "id = " + this.id.toString(), new String[] { });
		this.id = null;
		dba.close();
	}

}
